<?php
	/*
	* Nusaview Component for Joomla! 1.5.x
	* Version 1.0
	* Creation date: March 2009
	* Author: Angga Rajasa - Nur Aini Rakhmawati
	* Author email: rajasa.angga@gmail.com
	* License GNU/GPL
	*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view' );
class NvViewObjek extends JView{

	function display($tpl = null){
	
		//----------------------  an instance of JApplication
		global $mainframe;
		
		//----------------------   load data from objek's model
		$objek		=& $this->get('Data');
		$pageNav 	=& $this->get('Pagination');		
			
		if ($this->getLayout() == 'form') {	

			//----------------------   Setting up Toolbar for Propinsi's form
			$isNew = ($objek->id < 1);	
			$text = $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
			JToolBarHelper::save();
			if ($isNew)  {
				JToolBarHelper::cancel();
			} else {
				JToolBarHelper::cancel( 'cancel', 'Close' );
			}			

			//---------------------- load  an instance of JDatabase
			$db		=& JFactory::getDBO();
			//---------------------- make array for combobox
			$lists = array();
	
			//---------------------- load data if user request an edit form
			$row =& JTable::getInstance('nusaview', 'Table');
			$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
			$id = $cid[0];
			$row->load($id);		
		
			//---------------------- dynamic combobox javascript
			$javascript = "onchange=\"
			changeDynaList( 'kota_id', sectioncategories, document.adminForm.propinsi_id.options[document.adminForm.propinsi_id.selectedIndex].value, 0, 0);
			changeDynaList( 'id_bandara', sectioncategories2, document.adminForm.propinsi_id.options[document.adminForm.propinsi_id.selectedIndex].value, 0, 0);
			\"";
			
			//---------------------- get query propinsi all
			$query = 'SELECT s.propinsi_id, s.nama_propinsi' .
					' FROM #__propinsi AS s' .
					' ORDER BY s.propinsi_id';
			$db->setQuery($query);

			$propinsi_ids[] = JHTML::_('select.option', '-1', '- '.JText::_('Pilih Propinsi').' -', 'propinsi_id', 'nama_propinsi');
			$propinsi_ids = array_merge($propinsi_ids, $db->loadObjectList());
			$lists['propinsi_id'] = JHTML::_('select.genericlist',  $propinsi_ids, 'propinsi_id', 'class="inputbox" size="1" '.$javascript, 'propinsi_id' , 'nama_propinsi', intval($row->propinsi_id));		

			foreach ($propinsi_ids as $propinsi_id){
				$propinsi_list[] = (int) $propinsi_id->propinsi_id;
			}

			$propinsi_list = implode('\', \'', $propinsi_list);		
			
			//---------------------- get query kota 
			$sectioncategories = array ();	
			$query = 'SELECT kota_id,propinsi_id, nama_kota' .
					' FROM #__kota' .
					' WHERE propinsi_id IN ( \''.$propinsi_list.'\' )' .
					' ORDER BY kota_id';
			$db->setQuery($query);
			$cat_list = $db->loadObjectList();	
			
			foreach ($propinsi_ids as $propinsi_id){
				$sectioncategories[$propinsi_id->propinsi_id] = array ();
				$rows2 = array ();
				foreach ($cat_list as $cat)
				{
					if ($cat->propinsi_id == $propinsi_id->propinsi_id) {
						$rows2[] = $cat;
					}
				}
				foreach ($rows2 as $row2) {
					$sectioncategories[$propinsi_id->propinsi_id][] = JHTML::_('select.option', $row2->kota_id, $row2->nama_kota, 'kota_id', 'nama_kota');
				}
			}
			$kota_id = array();
			foreach ($cat_list as $cat) {
				if($cat->propinsi_id == $row->propinsi_id)
					$kota_id[] = $cat;
			}

			$kota_id[] = JHTML::_('select.option', '-1', JText::_( '- Pilih Kota -' ), 'kota_id', 'nama_kota');
			$lists['kota_id'] = JHTML::_('select.genericlist',  $kota_id, 'kota_id', 'class="inputbox" size="1"', 'kota_id', 'nama_kota', intval($row->kota_id));

			//---------------------- get query bandara's table 
			$sectioncategories2 = array ();		
			$query = 'SELECT id_bandara,nama_bandara,propinsi_id' .
					' FROM #__bandara' .
					' WHERE propinsi_id IN ( \''.$propinsi_list.'\' )' .
					' ORDER BY id_bandara';
			$db->setQuery($query);
			$bandara_list = $db->loadObjectList();			

			foreach ($propinsi_ids as $propinsi_id){
				$sectioncategories2[$propinsi_id->propinsi_id] = array ();
				$rows3 = array ();
				foreach ($bandara_list as $bandara)
				{
					if ($bandara->propinsi_id == $propinsi_id->propinsi_id) {
						$rows3[] = $bandara;
					}
				}
				foreach ($rows3 as $row3) {
					$sectioncategories2[$propinsi_id->propinsi_id][] = JHTML::_('select.option', $row3->id_bandara, $row3->nama_bandara, 'id_bandara', 'nama_bandara');
				}
			}		

			$id_bandara = array();
			foreach ($bandara_list as $bandara) {
				if($bandara->propinsi_id == $row->propinsi_id)
					$id_bandara[] = $bandara;
			}
			
			$id_bandara[] = JHTML::_('select.option', '-1', JText::_( '- Pilih Bandara -' ), 'id_bandara', 'nama_bandara');
			$lists['id_bandara'] = JHTML::_('select.genericlist',  $id_bandara, 'id_bandara', 'class="inputbox" size="1"', 'id_bandara', 'nama_bandara', intval($row->id_bandara));

			//--------------------- setting up configuration
			$config_params =& JComponentHelper::getParams( 'com_nv' );

			//----------------------   send to views as an object
			$this->assignRef('sectioncategories',	$sectioncategories);
			$this->assignRef('sectioncategories2',	$sectioncategories2);	
			$this->assignRef('config_params',	$config_params);		
			$this->assignRef('markers',	$markers);				
			$this->assignRef('lists',	$lists);		
			$this->assignRef('objek',	$objek);
			
		}
		else {
			//----------------------   Assign top menu of nusaview's component	
			JSubMenuHelper::addEntry( '<span class="editlinktip hasTip" title="'.JText::_( 'Objek Pariwisata' ).'::'.JText::_('Data-Data Pariwisata').'">'.JText::_( 'Objek Pariwisata' ).'</span>', 'index.php?option=com_nv&controller=objek', true);
			JSubMenuHelper::addEntry( '<span class="editlinktip hasTip" title="'.JText::_( 'Master Propinsi' ).'::'.JText::_('Master Data Propinsi').'">'.JText::_( 'Master Propinsi' ).'</span>', 'index.php?option=com_nv&controller=propinsi');
			JSubMenuHelper::addEntry( '<span class="editlinktip hasTip" title="'.JText::_( 'Master Kota' ).'::'.JText::_('Master Data Kota').'">'.JText::_( 'Master Kota' ).'</span>', 'index.php?option=com_nv&controller=kota');
			JSubMenuHelper::addEntry( '<span class="editlinktip hasTip" title="'.JText::_( 'Input Hotel' ).'::'.JText::_('Input Data Hotel').'">'.JText::_( 'Input Hotel' ).'</span>', 'index.php?option=com_nv&controller=form');
			JSubMenuHelper::addEntry( '<span class="editlinktip hasTip" title="'.JText::_( 'Input Restoran' ).'::'.JText::_('Input Data Restoran').'">'.JText::_( 'Input Restoran' ).'</span>', 'index.php?option=com_nv&controller=formrestoran');
			JSubMenuHelper::addEntry( '<span class="editlinktip hasTip" title="'.JText::_( 'Data Api Key' ).'::'.JText::_('Data Api Key').'">'.JText::_( 'Data Api Key' ).'</span>', 'index.php?option=com_nv&controller=apikey');
					
			
			//----------------------   Setting up Toolbar
			JHTML::_('behavior.tooltip');
			JToolBarHelper::title(   JText::_( 'Objek Pariwisata Nusantara View' ), 'generic.png' );
			JToolBarHelper::deleteList();
			JToolBarHelper::editListX();
			JToolBarHelper::addNewX();
			JToolBarHelper::preferences('com_nv', '350');
			

			//----------------------   send to views as an object
			$this->assignRef('objek',$objek);			
			$this->assignRef('pageNav', $pageNav);
			
		}
		//send to html
		parent::display($tpl);

	}// end of function
}// end of class
